import * as types from './types.js'
import Vue from 'vue'

export default {
  /*获取购物车列表*/
  [types.GET_ALL_CART](state, goods) {
    /*以key-value键值对的形式存放在购物车中*/
    state.cart = goods
  },
  [types.GET_ALL_CART_SELECTED](state, goods) {
    state.cartSelected = goods
  },
  /*添加商品到购物车中*/
  [types.CREATE_GOODS_TO_CART](state, goods) {
    /*以key-value键值对的形式存放在购物车中*/
    Vue.set(state.cart, goods.allbusinessId, goods)
  },
  /*添加商品到购物车选中列表*/
  [types.CREATE_GOODS_TO_SELECTED](state, goods) {
    /*以key-value键值对的形式存放在购物车中*/
    Vue.set(state.cartSelected, goods.allbusinessId, goods)
    state.cart[goods.allbusinessId].selectStatus = '1'
  },

  /*更新购物车促销信息*/
  [types.UPDATE_GOODS_COUPONID](state, goods) {
    state.cart[goods.allbusinessId].couponId = goods.couponId
    if (state.cartSelected[goods.allbusinessId]) {
      state.cart[goods.allbusinessId].couponId = goods.couponId
    }
  },
  /*删除购物车里的商品*/
  [types.DELETE_GOODS_FROM_CART](state, goods) {
    /*删除商品只要删除指定的key*/
    Vue.delete(state.cart, goods.allbusinessId)
  },
  /*删除购物车里选中的商品*/
  [types.DELETE_GOODS_FROM_SELECTED](state, goods) {
    /*删除商品只要删除指定的key*/
    Vue.delete(state.cartSelected, goods.allbusinessId)
    state.cart[goods.allbusinessId].selectStatus = '0'
  },
  /*购物车的商品+1*/
  [types.ADD_GOODS_FROM_CART](state, goods) {
    let item = state.cart[goods.allbusinessId]
    let amount = item.amount
    /*Obejct.assign浅拷贝，会对对象里存在的属性覆盖，不存在的属性添加*/
    state.cart[goods.allbusinessId] = Object.assign({}, item, { amount: amount + 1 })
    // state.cart = Object.assign({ [goods.allbusinessId]: '' }, state.cart) // 将购物车更新的商品排到第一位
  },
  /*购物车选中的商品+1*/
  [types.ADD_GOODS_FROM_SELECTED](state, goods) {
    let item = state.cartSelected[goods.allbusinessId]
    let amount = item.amount
    /*Obejct.assign浅拷贝，会对对象里存在的属性覆盖，不存在的属性添加*/
    state.cartSelected[goods.allbusinessId] = Object.assign({}, item, { amount: amount + 1 })
  },
  /*购物车的商品-1*/
  [types.REDUCE_GOODS_FROM_CART](state, goods) {
    let item = state.cart[goods.allbusinessId]
    let amount = item.amount
    if (amount <= 1) return
    state.cart[goods.allbusinessId] = Object.assign({}, item, { amount: amount - 1 })
    // state.cart = Object.assign({ [goods.allbusinessId]: '' }, state.cart)
  },
  /*购物车选中的商品-1*/
  [types.REDUCE_GOODS_FROM_SELECTED](state, goods) {
    // console.log(goods)
    let item = state.cartSelected[goods.allbusinessId]
    let amount = item.amount
    if (amount <= 1) return
    state.cartSelected[goods.allbusinessId] = Object.assign({}, item, { amount: amount - 1 })
  },
  /*修改购物车的商品的数量*/
  [types.MODIFY_GOODS_NUM_FROM_CART](state, data) {
    let goods = data.goods
    let amount = data.amount
    let item = state.cart[goods.allbusinessId]

    /*购买次数限制的逻辑这里不写，有需要自己添加*/
    /*更新*/
    state.cart[goods.allbusinessId] = Object.assign({}, item, { amount: amount })
  },
  /*更新购物车*/

  [types.UPDATE_CART](state) {
    // console.log(state.cart)
    // console.log(Object.keys(state.cart).length)
    // console.log(JSON.stringify(state.cart))
    localStorage.setItem('cart', JSON.stringify(state.cart))
    localStorage.setItem('cartSelected', JSON.stringify(state.cartSelected))
  }
}
